home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MacToggleButtonBorder.java < prev    next >
Text File  |  1998-06-30  |  13KB  |  391 lines

  1. /*
  2.  * %W% %E%
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.mac;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.border.*;
  25. import com.sun.java.swing.plaf.*;
  26.  
  27. import java.awt.Component;
  28. import java.awt.Insets;
  29. import java.awt.Color;
  30. import java.awt.Graphics;
  31. import java.awt.Image;
  32. import java.awt.Point;
  33. import java.awt.Dimension;
  34. import java.util.Vector;
  35.  
  36. /**
  37.  * @version @(#)MacToggleButtonBorder.java    1.0 12/5/97
  38.  * @author Symantec
  39.  * @author Levi Brown
  40.  */
  41.  
  42. class MacToggleButtonBorder extends AbstractBorder implements UIResource
  43. {
  44.     protected static final Insets    smallInsets        = new Insets(2, 2, 2, 2);
  45.     protected static final Insets    mediumInsets    = new Insets(3, 3, 3, 3);
  46.     protected static final Insets    largeInsets        = new Insets(4, 4, 4, 4);
  47.  
  48.     protected static final int        smallThreshold    = 25;
  49.     protected static final int        largeThreshold    = 35;
  50.     protected static final int        SMALL_SIZE        = 1;
  51.     protected static final int        MEDIUM_SIZE        = 2;
  52.     protected static final int        LARGE_SIZE        = 3;
  53.  
  54.     protected static Color GSA1Color    = UIManager.getColor("GrayscaleAppearanceA1");
  55.     protected static Color GSA2Color    = UIManager.getColor("GrayscaleAppearanceA2");
  56.     protected static Color GSWColor    = UIManager.getColor("GrayscaleAppearanceW");
  57.     protected static Color GS1Color    = UIManager.getColor("GrayscaleAppearance1");
  58.     protected static Color GS2Color    = UIManager.getColor("GrayscaleAppearance2");
  59.     protected static Color GS3Color    = UIManager.getColor("GrayscaleAppearance3");
  60.     protected static Color GS4Color    = UIManager.getColor("GrayscaleAppearance4");
  61.     protected static Color GS5Color    = UIManager.getColor("GrayscaleAppearance5");
  62.     protected static Color GS6Color    = UIManager.getColor("GrayscaleAppearance6");
  63.     protected static Color GS7Color    = UIManager.getColor("GrayscaleAppearance7");
  64.     protected static Color GS8Color    = UIManager.getColor("GrayscaleAppearance8");
  65.     protected static Color GS9Color    = UIManager.getColor("GrayscaleAppearance9");
  66.     protected static Color GS10Color    = UIManager.getColor("GrayscaleAppearance10");
  67.     protected static Color GS11Color    = UIManager.getColor("GrayscaleAppearance11");
  68.  
  69.     protected static ImageIcon ldOnTB        = null;
  70.     protected static ImageIcon ldOffTB;
  71.     protected static ImageIcon lOnTB;
  72.     protected static ImageIcon lPressedTB;
  73.     protected static ImageIcon lOffTB;
  74.     protected static ImageIcon mdOnTB;
  75.     protected static ImageIcon mdOffTB;
  76.     protected static ImageIcon mOnTB;
  77.     protected static ImageIcon mPressedTB;
  78.     protected static ImageIcon mOffTB;
  79.     protected static ImageIcon sdOnTB;
  80.     protected static ImageIcon sdOffTB;
  81.     protected static ImageIcon sOnTB;
  82.     protected static ImageIcon sPressedTB;
  83.     protected static ImageIcon sOffTB;
  84.  
  85.     protected int buttonType;
  86.     
  87.     public MacToggleButtonBorder()
  88.     {
  89.         if(ldOnTB == null)
  90.         {
  91.             ldOnTB        = (ImageIcon) UIManager.getIcon("ToggleButton.largeDisabledOn");
  92.             ldOffTB        = (ImageIcon) UIManager.getIcon("ToggleButton.largeDisabledOff");
  93.             lOnTB        = (ImageIcon) UIManager.getIcon("ToggleButton.largeOn");
  94.             lPressedTB    = (ImageIcon) UIManager.getIcon("ToggleButton.largePressed");
  95.             lOffTB        = (ImageIcon) UIManager.getIcon("ToggleButton.largeOff");
  96.             mdOnTB        = (ImageIcon) UIManager.getIcon("ToggleButton.mediumDisabledOn");
  97.             mdOffTB        = (ImageIcon) UIManager.getIcon("ToggleButton.mediumDisabledOff");
  98.             mOnTB        = (ImageIcon) UIManager.getIcon("ToggleButton.mediumOn");
  99.             mPressedTB    = (ImageIcon) UIManager.getIcon("ToggleButton.mediumPressed");
  100.             mOffTB        = (ImageIcon) UIManager.getIcon("ToggleButton.mediumOff");
  101.             sdOnTB        = (ImageIcon) UIManager.getIcon("ToggleButton.smallDisabledOn");
  102.             sdOffTB        = (ImageIcon) UIManager.getIcon("ToggleButton.smallDisabledOff");
  103.             sOnTB        = (ImageIcon) UIManager.getIcon("ToggleButton.smallOn");
  104.             sPressedTB    = (ImageIcon) UIManager.getIcon("ToggleButton.smallPressed");
  105.             sOffTB        = (ImageIcon) UIManager.getIcon("ToggleButton.smallOff");
  106.         }
  107.     }
  108.     
  109.     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
  110.     {
  111.         boolean    isPressed    = false;
  112.         boolean    isSelected    = false;
  113.         boolean    isEnabled    = true;
  114.         boolean    isActive    = true;
  115.         Vector        colors        = new Vector();
  116.         ImageIcon    workingIcon    = null;
  117.         
  118.         if (c instanceof AbstractButton)
  119.         {
  120.             AbstractButton b = (AbstractButton)c;
  121.             ButtonModel model = b.getModel();
  122.             
  123.             isPressed    = (model.isArmed() && model.isPressed());
  124.             isSelected    = model.isSelected();
  125.             isEnabled    = model.isEnabled();
  126.  
  127.             ComponentUI ui = b.getUI();
  128.             if(ui instanceof MacToggleButtonUI)
  129.             {
  130.                 isActive = ((MacToggleButtonUI)ui).isActive();
  131.             }
  132.         }
  133.         
  134.         
  135.         determineButtonType(width, height);
  136.         
  137.         switch (buttonType)
  138.         {
  139.             case SMALL_SIZE:
  140.                 if(isEnabled && isActive)
  141.                 {
  142.                     if(isPressed)
  143.                     {
  144.                         workingIcon        = sPressedTB;
  145.         
  146.                         colors.addElement(GSA2Color);    //Top and Left
  147.                         colors.addElement(GS11Color);    //Bottom and Right
  148.                         colors.addElement(GS10Color);    //Top and Left
  149.                         colors.addElement(GS5Color);    //Bottom and Right
  150.                     }
  151.                     //If it's released
  152.                     else
  153.                     {
  154.                         if(isSelected)
  155.                         {
  156.                             workingIcon        = sOnTB;
  157.                             
  158.                             colors.addElement(GSA2Color);    //Top and Left
  159.                             colors.addElement(GS11Color);    //Bottom and Right
  160.                             colors.addElement(GS10Color);    //Top and Left
  161.                             colors.addElement(GS5Color);    //Bottom and Right
  162.                         }
  163.                         //If it's off
  164.                         else
  165.                         {
  166.                             workingIcon        = sOffTB;
  167.                             
  168.                             colors.addElement(GS9Color);    //Top and Left
  169.                             colors.addElement(GSA1Color);    //Bottom and Right
  170.                             colors.addElement(GSWColor);    //Top and Left
  171.                             colors.addElement(GS7Color);    //Bottom and Right
  172.                         }
  173.                     }
  174.                 }
  175.                 //Disabled
  176.                 else
  177.                 {
  178.                         if(isSelected)
  179.                         {
  180.                             workingIcon        = sdOnTB;
  181.                             
  182.                             colors.addElement(GS8Color);    //Top and Left
  183.                             colors.addElement(GS6Color);    //Bottom and Right
  184.                             colors.addElement(GS4Color);    //Top and Left
  185.                             colors.addElement(GS4Color);    //Bottom and Right
  186.                         }
  187.                         //If it's off
  188.                         else
  189.                         {
  190.                             workingIcon        = sdOffTB;
  191.                             
  192.                             colors.addElement(GS5Color);    //Top and Left
  193.                             colors.addElement(GS6Color);    //Bottom and Right
  194.                             colors.addElement(GS2Color);    //Top and Left
  195.                             colors.addElement(GS2Color);    //Bottom and Right
  196.                         }
  197.                 }
  198.                 break;
  199.                 
  200.             case MEDIUM_SIZE:
  201.                 if(isEnabled && isActive)
  202.                 {
  203.                     if(isPressed)
  204.                     {
  205.                         workingIcon        = mPressedTB;
  206.         
  207.                         colors.addElement(GSA2Color);    //Top and Left
  208.                         colors.addElement(GS11Color);    //Bottom and Right
  209.                         colors.addElement(GS11Color);    //Top and Left
  210.                         colors.addElement(GS4Color);    //Bottom and Right
  211.                         colors.addElement(GS9Color);    //Top and Left
  212.                         colors.addElement(GS6Color);    //Bottom and Right
  213.                     }
  214.                     //If it's released
  215.                     else
  216.                     {
  217.                         if(isSelected)
  218.                         {
  219.                             workingIcon        = mOnTB;
  220.                             
  221.                             colors.addElement(GSA2Color);    //Top and Left
  222.                             colors.addElement(GS11Color);    //Bottom and Right
  223.                             colors.addElement(GS11Color);    //Top and Left
  224.                             colors.addElement(GS4Color);    //Bottom and Right
  225.                             colors.addElement(GS9Color);    //Top and Left
  226.                             colors.addElement(GS6Color);    //Bottom and Right
  227.                         }
  228.                         //If it's off
  229.                         else
  230.                         {
  231.                             workingIcon        = mOffTB;
  232.                             
  233.                             colors.addElement(GS9Color);    //Top and Left
  234.                             colors.addElement(GSA1Color);    //Bottom and Right
  235.                             colors.addElement(GS3Color);    //Top and Left
  236.                             colors.addElement(GS8Color);    //Bottom and Right
  237.                             colors.addElement(GSWColor);    //Top and Left
  238.                             colors.addElement(GS6Color);    //Bottom and Right
  239.                         }
  240.                     }
  241.                 }
  242.                 //Disabled
  243.                 else
  244.                 {
  245.                         if(isSelected)
  246.                         {
  247.                             workingIcon        = mdOnTB;
  248.                             
  249.                             colors.addElement(GS8Color);    //Top and Left
  250.                             colors.addElement(GS6Color);    //Bottom and Right
  251.                             colors.addElement(GS4Color);    //Top and Left
  252.                             colors.addElement(GS4Color);    //Bottom and Right
  253.                             colors.addElement(GS4Color);    //Top and Left
  254.                             colors.addElement(GS4Color);    //Bottom and Right
  255.                         }
  256.                         //If it's off
  257.                         else
  258.                         {
  259.                             workingIcon        = mdOffTB;
  260.                             
  261.                             colors.addElement(GS5Color);    //Top and Left
  262.                             colors.addElement(GS6Color);    //Bottom and Right
  263.                             colors.addElement(GS2Color);    //Top and Left
  264.                             colors.addElement(GS2Color);    //Bottom and Right
  265.                             colors.addElement(GS2Color);    //Top and Left
  266.                             colors.addElement(GS2Color);    //Bottom and Right
  267.                         }
  268.                 }
  269.                 break;
  270.                 
  271.             case LARGE_SIZE:
  272.                 if(isEnabled && isActive)
  273.                 {
  274.                     if(isPressed)
  275.                     {
  276.                         workingIcon        = lPressedTB;
  277.         
  278.                         colors.addElement(GSA2Color);    //Top and Left
  279.                         colors.addElement(GS11Color);    //Bottom and Right
  280.                         colors.addElement(GSA1Color);    //Top and Left
  281.                         colors.addElement(GS4Color);    //Bottom and Right
  282.                         colors.addElement(GS11Color);    //Top and Left
  283.                         colors.addElement(GS5Color);    //Bottom and Right
  284.                         colors.addElement(GS9Color);    //Top and Left
  285.                         colors.addElement(GS6Color);    //Bottom and Right
  286.                     }
  287.                     //If it's released
  288.                     else
  289.                     {
  290.                         if(isSelected)
  291.                         {
  292.                             workingIcon        = lOnTB;
  293.                             
  294.                             colors.addElement(GSA2Color);    //Top and Left
  295.                             colors.addElement(GS11Color);    //Bottom and Right
  296.                             colors.addElement(GSA1Color);    //Top and Left
  297.                             colors.addElement(GS4Color);    //Bottom and Right
  298.                             colors.addElement(GS11Color);    //Top and Left
  299.                             colors.addElement(GS5Color);    //Bottom and Right
  300.                             colors.addElement(GS9Color);    //Top and Left
  301.                             colors.addElement(GS6Color);    //Bottom and Right
  302.                         }
  303.                         //If it's off
  304.                         else
  305.                         {
  306.                             workingIcon        = lOffTB;
  307.                             
  308.                             colors.addElement(GS9Color);    //Top and Left
  309.                             colors.addElement(GSA1Color);    //Bottom and Right
  310.                             colors.addElement(GS3Color);    //Top and Left
  311.                             colors.addElement(GS10Color);    //Bottom and Right
  312.                             colors.addElement(GS1Color);    //Top and Left
  313.                             colors.addElement(GS8Color);    //Bottom and Right
  314.                             colors.addElement(GSWColor);    //Top and Left
  315.                             colors.addElement(GS6Color);    //Bottom and Right
  316.                         }
  317.                     }
  318.                 }
  319.                 //Disabled
  320.                 else
  321.                 {
  322.                         if(isSelected)
  323.                         {
  324.                             workingIcon        = ldOnTB;
  325.                             
  326.                             colors.addElement(GS8Color);    //Top and Left
  327.                             colors.addElement(GS6Color);    //Bottom and Right
  328.                             colors.addElement(GS4Color);    //Top and Left
  329.                             colors.addElement(GS4Color);    //Bottom and Right
  330.                             colors.addElement(GS4Color);    //Top and Left
  331.                             colors.addElement(GS4Color);    //Bottom and Right
  332.                             colors.addElement(GS4Color);    //Top and Left
  333.                             colors.addElement(GS4Color);    //Bottom and Right
  334.                         }
  335.                         //If it's off
  336.                         else
  337.                         {
  338.                             workingIcon        = ldOffTB;
  339.                             
  340.                             colors.addElement(GS5Color);    //Top and Left
  341.                             colors.addElement(GS7Color);    //Bottom and Right
  342.                             colors.addElement(GS2Color);    //Top and Left
  343.                             colors.addElement(GS2Color);    //Bottom and Right
  344.                             colors.addElement(GS2Color);    //Top and Left
  345.                             colors.addElement(GS2Color);    //Bottom and Right
  346.                             colors.addElement(GS2Color);    //Top and Left
  347.                             colors.addElement(GS2Color);    //Bottom and Right
  348.                         }
  349.                 }
  350.                 break;
  351.         }
  352.         
  353.         MacBorderFactory.paintButtonBorder(c, g, workingIcon, colors, new Point(0, 0), new Dimension(width, height));
  354.     }
  355.     
  356.     public Insets getBorderInsets(Component c)
  357.     {
  358.         determineButtonType(c);
  359.         
  360.         switch (buttonType)
  361.         {
  362.             case SMALL_SIZE:
  363.                 return smallInsets;
  364.             case MEDIUM_SIZE:
  365.                 return mediumInsets;
  366.             case LARGE_SIZE:
  367.                 return largeInsets;
  368.         }
  369.         
  370.         //We should never get here.
  371.         return mediumInsets;
  372.     }
  373.     
  374.     protected void determineButtonType(int width, int height)
  375.     {
  376.         //Determine what type of button border to draw, based on the button's size
  377.         int maxDim = Math.max(width, height);
  378.         if(maxDim < smallThreshold)
  379.             buttonType = SMALL_SIZE;
  380.         else if(maxDim >= largeThreshold)
  381.             buttonType = LARGE_SIZE;
  382.         else
  383.             buttonType = MEDIUM_SIZE;
  384.     }
  385.     
  386.     protected void determineButtonType(Component c)
  387.     {
  388.         determineButtonType(c.getSize().width, c.getSize().height);
  389.     }
  390. }    
  391.